﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace employee.Controls
{
    public partial class emRelative : common.controls.baseListEdit
    {
        public enum ColumnNames
        {
            FullName, Type, Address
        };

        private data.baseDS.emRelativesDataTable myDataTbl = new data.baseDS.emRelativesDataTable();
        public emRelative()
        {
            try
            {
                InitializeComponent();
            }
            catch (Exception er)
            {
                ErrorHandler(this, er);
            }            
        }

        public override event common.myEventHandler myOnError
        {
            add
            {
                base.myOnError += value;
                detailObj.myOnError += value;
            }
            remove
            {
                base.myOnError -= value;
                detailObj.myOnError -= value;
            }
        }
        public override void Init()
        {
            base.Init();
            this.myDataTbl.Clear();
            this.gridDataSource.DataSource = this.myDataTbl;
            detailObj.Init(this.gridDataSource);
            SetData(myDataTbl);
        }
        public override void LoadData(string emCode) 
        {
            myDataTbl.Clear();
            if (emCode != null)
            {
                DataAccess.DataLibs.LoadData(myDataTbl, emCode);
            }
        }
        public override void SaveData(string emCode)
        {
            gridDataSource.EndEdit();
            for (int idx = 0; idx < myDataTbl.Count; idx++)
            {
                if (myDataTbl[idx].RowState == DataRowState.Deleted) continue;
                myDataTbl[idx].emCode = emCode;
            }
            DataAccess.DataLibs.UpdateData(myDataTbl);
        }
        public override void LockEdit(bool state)
        {
            detailObj.LockEdit(state);
            base.LockEdit(state);
        }
        public override bool CheckData()
        {
            return detailObj.CheckData();
        }
        public override void SetFocus()
        {
            detailObj.SetFocus();
        }

        private void SetData(data.baseDS.emRelativesDataTable tbl)
        {
            // 
            // fullName
            // 
            DataGridViewTextBoxColumn fullName = new DataGridViewTextBoxColumn();
            fullName.Name = ColumnNames.FullName.ToString();
            fullName.DataPropertyName = tbl.fullNameColumn.ColumnName;
            fullName.HeaderText = "Họ và tên";
            fullName.Name = tbl.fullNameColumn.ColumnName;
            fullName.ReadOnly = true;

            // type
            DataAccess.DataCache.LoadFamilyRelaType(false);
            BindingSource relationTypeSrc = new BindingSource();
            relationTypeSrc.DataSource = DataAccess.DataCache.myBaseDS.userType;

            DataGridViewComboBoxColumn type = new DataGridViewComboBoxColumn();
            type.DataSource = relationTypeSrc;
            type.DataPropertyName = tbl.relativeCodeColumn.ColumnName;
            type.DisplayMember = DataAccess.DataCache.myBaseDS.familyRelaType.descriptionColumn.ColumnName;
            type.ValueMember = DataAccess.DataCache.myBaseDS.familyRelaType.codeColumn.ColumnName;
            type.HeaderText = "Loại";
            type.Name = ColumnNames.Type.ToString();
            type.ReadOnly = true;

            type.Width = 120;
            type.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            type.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            // 
            // Address
            // 
            DataGridViewTextBoxColumn address = new DataGridViewTextBoxColumn();
            address.Name = ColumnNames.Address.ToString();
            address.DataPropertyName = tbl.addressColumn.ColumnName;
            address.HeaderText = "Địa chỉ";
            address.Name = tbl.addressColumn.ColumnName;
            address.ReadOnly = true;
            address.Visible = false;

            //Grid
            this.detailGrid.Columns.Clear();
            this.detailGrid.Columns.AddRange(new DataGridViewColumn[] { fullName, type, address });

            this.detailGrid.myAutoFitColumn = ColumnNames.FullName.ToString();
            this.detailGrid.AutoFit();
        }
        public override void AddNew()
        {
            data.baseDS.emRelativesRow row;
            string saveType = ""; 
            if (this.gridDataSource.Current != null )
            {
                row = (data.baseDS.emRelativesRow)((DataRowView)this.gridDataSource.Current).Row;
                saveType = row.relationType; 
            }
            gridDataSource.AddNew();
            if (this.gridDataSource.Current != null)
            {
                row = (data.baseDS.emRelativesRow)((DataRowView)this.gridDataSource.Current).Row;
                DataAccess.DataLibs.InitData(row);
                row.relationType = saveType;
            }
        }
    }
}
